package Thread.New.等待与唤醒;
@SuppressWarnings("ALL")
public class WaitandWake {
    public static void main(String[] args) {
        Object obj = new Object();

        //顾客线程
        new Thread(){
            @Override
            public void run() {
                //保证等待和唤醒只有一个线程
                while (true) {
                    synchronized (obj) {
                        System.out.println("告诉老板我要吃包子");
                        //
                        try {
                            obj.wait();
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                        //唤醒之后
                        System.out.println("我醒来了,我开始吃包子");
                        System.out.println("-------------------");
                    }
                }
            }
        }.start();

        //老板线程
        new Thread(){
            @Override
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        System.out.println(e);
                    }

                    synchronized (obj) {
                        //5s之后告知顾客做好了
                        System.out.println("包子好了");
                        obj.notify();
                        //唤醒睡眠的顾客
                    }
                }
            }
        }.start();
    }
}
